package com.wfit.controller.admin;

import com.alibaba.fastjson.JSON;
import com.wfit.dto.OrderPageDTO;
import com.wfit.dto.OrderStatusDTO;
import com.wfit.po.Order;
import com.wfit.result.Result;
import com.wfit.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.wfit.result.Result.success;

@RestController
@RequestMapping("/orders")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 获取所有订单信息
     *
     * @return 订单列表
     */
    @GetMapping
    public Result findAll() {
        List<Order> list = orderService.list();
        return success(list);
    }

    /**
     * 根据ID获取订单信息
     *
     * @param id 订单ID
     * @return 订单信息
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        Order byId = orderService.getById(id);
        return success(byId);
    }

    /**
     * 插入新的订单信息
     *
     * @param order 订单对象
     */
    @PostMapping
    public Result insert(@RequestBody Order order) {
        orderService.save(order);
        return Result.success();
    }

    /**
     * 更新订单信息
     *
     * @param order 订单对象
     */
    @PutMapping
    public Result update(@RequestBody Order order) {
        orderService.updateById(order);
        return Result.success();
    }

    /**
     * 根据ID删除订单信息
     *
     * @param id 订单ID
     */
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id) {
        orderService.removeById(id);
        return Result.success();
    }

    /**
     * 根据条件搜索订单信息
     *
     * @param dto
     * @return 订单列表
     */
    @PostMapping("/search")
    public Result search(@RequestBody OrderPageDTO dto) {

        Result search = orderService.search(dto);
        return Result.success(search);
    }

    /**
     * 接受订单
     *
     * @param dto 订单ID
     */
    @PutMapping("/{id}")
    public void orderStatus(@RequestBody OrderStatusDTO dto) {
        orderService.orderStatus(dto);
    }

}
